/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Page;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author Vince
 */
public class HomepageManager extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        SessionFactory sessionFactory = SchoolContentManager.sessionFactory;
        Session session = sessionFactory.getCurrentSession(); //get the session from the sessionFactory
        Transaction transaction = session.beginTransaction(); //start the transaction
        HttpSession sessie = request.getSession();
        Page page = (Page) session.get(Page.class, "0");

        page.setTitle(request.getParameter("title"));
        page.setSubtitle(request.getParameter("subtitle"));
        page.setMessage(request.getParameter("message").replaceAll("\n", "<br>"));

        session.saveOrUpdate(page);
        transaction.commit();
        
        sessie.setAttribute("hpmsg", "<b>Change Succesful!</b>");
        sessie.setAttribute("title", page.getTitle());
        sessie.setAttribute("subtitle", page.getSubtitle());
        sessie.setAttribute("message", page.getMessage().replaceAll("\n", "<br>"));
        sessie.setAttribute("titlee", page.getTitle());
        sessie.setAttribute("subtitlee", page.getSubtitle());
        sessie.setAttribute("messagee", page.getMessage().replaceAll("<br>", "\n"));
        sessie.setAttribute("contentPage", "/manageHomepage.jsp");
        addHistori(sessie, "/manageHomepage.jsp");

        response.sendRedirect("index");
    }

    private void addHistori(HttpSession sessie, Object attribute) {
        List<Object> historie;
        if (sessie.getAttribute("lastpage") == null) {
            historie = new ArrayList();
        } else {
            historie = (ArrayList<Object>) sessie.getAttribute("lastpage");
        }
        System.out.println("terug naar" + attribute);
        historie.add(attribute);

        sessie.setAttribute("lastpage", historie);
    }

    private Object getLast(HttpSession sessie) {
        List<Object> historie = (List<Object>) sessie.getAttribute("lastpage");
        Object re = historie.get(historie.size() - 1);
        historie.remove(re);
        return re;
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
